cd ${work}5_reg_analysis


*** FDY LEVEL
local ylist		"exp_status log_vrd_exp_exsmall" 
local felist	`" "IDY FD FY" "'
local wlist		`" "" "'
local plist		"1 2 3 4 5"

use temp\regsample_product_idt, clear

eststo clear
local mtitles
local n=1
foreach y of local ylist {

local nfe=1	
foreach fe of local felist {
local w : word `nfe' of `wlist'

local estlist_iv

foreach p of local plist {
    
	cap drop otherx
	preserve
		keep treated_p*_2
		ds treated_p`p'_2, not
	restore
	egen otherx = rowmax(`r(varlist)')		
		
	cap gen x_iv_p`p' = .	
	replace x_iv_p`p' = treated_p`p'_2
	lab var x_iv_p`p' "`:var lab treated_p`p'_2'"
	
	eststo est_iv_`p' : ivreghdfe `y' ( x_iv_p`p' = tdcinitiative_2 ) `w' if otherx==0 | treated_p`p'_2==1, a(`fe') cl(F)
	local estlist_iv `estlist_iv' est_iv_`p'	
	local N`p' = e(N)
			
}

	eststo stacked_`n' : appendmodels `estlist_iv'
foreach p of local plist {
	qui estadd scalar N`p' `N`p''
}	
	qui estadd local FE `fe'
	if "`w'"!=""	qui estadd local SC "Yes"
	if "`w'"==""	qui estadd local SC "No"
	qui estadd local LO "FDY"
	local mtitles `mtitles' "`:var lab `y''"	
	eststo drop est*	

	local ++n
	local ++nfe
	
}	
}


*** FPDY LEVEL
local ylist		"log_quantity log_price"
local felist	`" "IDY FPD FPY" "'
local wlist		`" "" "'
local plist		"1 2 3 4 5"

use temp\regsample_product_ipdt, clear

foreach y of local ylist {

local nfe=1	
foreach fe of local felist {
local w : word `nfe' of `wlist'

local estlist_iv
foreach p of local plist {
    
	cap drop otherx
	preserve
		keep treated_p*_2
		ds treated_p`p'_2, not
	restore
	egen otherx = rowmax(`r(varlist)')		
	
	cap gen x_iv_p`p' = .	
	replace x_iv_p`p' = treated_p`p'_2
	lab var x_iv_p`p' "`:var lab treated_p`p'_2'"
	
	eststo est_iv_`p' : ivreghdfe `y' ( x_iv_p`p' = tdcinitiative_2 ) `w' if otherx==0 | treated_p`p'_2==1, a(`fe') cl(F)
	local estlist_iv `estlist_iv' est_iv_`p'	
	local N`p' = e(N)
	
}

	eststo stacked_`n' : appendmodels `estlist_iv'
foreach p of local plist {
	qui estadd scalar N`p' `N`p''
}	
	qui estadd local FE `fe'
	if "`w'"!=""	qui estadd local SC "Yes"
	if "`w'"==""	qui estadd local SC "No"
	qui estadd local LO "FPDY"	
	local mtitles `mtitles' "`:var lab `y''"	
	eststo drop est*	

	local ++n
	local ++nfe
	
}	
}

*** TABLE
esttab using output\table_a6.tex, replace booktabs se label ///
	title("2SLS Effects of Export Support on Trade across Export Support Groups") star(* 0.1 ** 0.05 *** 0.01) keep(x_*) mtitles(`mtitles') ///
	stats(N1 N2 N3 N4 N5 LO FE SC , labels("N, Partner Search etc." "N, Fairs etc." "N, Intelligence etc." "N, Commercial Assistance" "N, Everything else" "Unit of observation" "Fixed Effects" "Size Control") fmt(%10.0fc %10.0fc %10.0fc %10.0fc %10.0fc "" "" ""))
	